//
//  GetCodeVC.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/8/12.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//
//  Roger:18684909663

import UIKit
import YSKit
import TSMessages

class GetCodeVC: YSBaseViewController {
    var time: Int = 0
    var timer: Timer!
    var code = ""

    @IBOutlet weak var tf_phone: UITextField!
    @IBOutlet weak var tf_code: UITextField!
    @IBOutlet weak var btn_code: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = I18n("VC.login.forgetpwd.title")
        tf_phone.changePlaceholderColor(UIColor.init(red: 65, green: 181, blue: 116))
        tf_code.changePlaceholderColor(UIColor.init(red: 65, green: 181, blue: 116))

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func getCodeAction(_ sender: UIButton) {
        if !(tf_phone.text?.isPhone("CN"))! {
            TSMessage.showNotification(withTitle: I18n("VC.login.notphone"), type: .warning)
            return
        }
        
        HUD.show()
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.getCode(phone: self.tf_phone.text!))).subscribe(onNext: { [weak self] (data) in
            //
            HUD.hide()
            guard let `self` = self, let `data` = data else {
                return
            }

            Auth.token = (data["token"].string)!
            print("code:\(data["data"]["code"].intValue)")
            self.code = "\(data["data"]["code"].intValue)"
            sender.isEnabled = false
            self.time = 60
            self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { (Timer) in
                self.btn_code.setTitle(String.init(format: "%lds", self.time), for: UIControlState.normal)
                if self.time == 0 {
                    self.time = 60
                    self.btn_code .setTitle(I18n("VC.login.verifybtn.title"), for: UIControlState.normal)
                    self.btn_code.isEnabled = true
                    self.timer.invalidate()
                }else{
                    self.time -= 1
                }
            })
            
            }, onError: { [weak self](ErrorType) in
                HUD.hide()
                guard let  `self` = self else {
                    return
                }
                self.handle(withError: ErrorType)
        })
        
    }

    @IBAction func nextStepAction(_ sender: UIButton) {
        if !(tf_phone.text?.isPhone("CN"))! {
            TSMessage.showNotification(withTitle: I18n("VC.login.notphone"), type: .warning)
            return;
        }
        
        if self.tf_code.text?.characters.count == 0 {
            TSMessage.showNotification(withTitle: I18n("VC.login.nocode"), type: .warning)
            return;
        }
        
        if self.code.characters.count == 0 {
            TSMessage.showNotification(withTitle: I18n("VC.login.nogetcode"), type: .warning)
            return;
        }
        
        if self.code != tf_code.text {
            TSMessage.showNotification(withTitle: I18n("VC.login.failcode"), type: .warning)
            return;
        }
        
        self.performSegue(withIdentifier: "ForgetPwdVC", sender:nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ForgetPwdVC"{
            let controller = segue.destination as! ForgetPwdVC
            controller.code = tf_code.text!
            controller.phone = tf_phone.text!
        }
    }

}
